package com.no1.zy.controller.user;

import com.github.pagehelper.PageInfo;
import com.no1.zy.pojo.User;
import com.no1.zy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@Slf4j
class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("toupdate/{id}")
    public String toupdate(@PathVariable Long id, Model model) {
        log.info("准备跳转到用户信息修改页...");
        // 根据id查找到，我们要修改的那条用户记录，
        User user = userService.queryById(id);
        // 封装user
        model.addAttribute("user", user);
        // 并导航到update.html页面
        return "user/update";
    }

    // 添加一个修改用户的方法
    @RequestMapping("update")
    public String update(User user) {
        log.info("拿到前台传递过来的数据 user -> " + user);
        log.info("准备调用后台修改用户信息业务...");
        userService.modifyUser(user);
        // 导航到查询模块
        return "redirect:/user/showAll";
    }

    // 查询
    @RequestMapping("show/{id}")
    public String show(@PathVariable Long id, Model model) {
        User user = userService.queryById(id);
        model.addAttribute("user", user);
        return "user/show";
    }

    // 跳转新增用户页面
    @RequestMapping("toinsert")
    public String toInsert() {
        log.debug("即将跳转到新添用户界面...");
        return "redirect:/user/insert";
    }

    // 添加一个新增用户的方法
    @RequestMapping("insert")
    public String insert(User user) {
        log.info("接收到前台传来的数据：user -> " + user);
        userService.saveUser(user);
        return "user/insert";
    }

    // 添加一个查询所有用户的方法
    @RequestMapping("showAll")
    public String showAll(Model model) {
        log.info("准备调用后台查询所有用户的业务");
        List<User> userList = userService.queryAll();
        model.addAttribute("userList", userList);
        return "user/showAll";
    }

    @RequestMapping("deleteById/{id}")
    public String deleteById(@PathVariable Long id) {
        log.debug("准备删除数据...");
        userService.removeUser(id);
        return "redirect:/user/showAll";
    }

    @Value("${page.records}")
    String pageRecords;

    @RequestMapping("queryByPage/{curPage}")
    public String queryByPage(@PathVariable Integer curPage,Model model){
     log.debug("执行分页查询结果...");
     if (curPage < 1)
         curPage=1;
        PageInfo<User> pageInfo=userService.queryByPage(curPage,Integer.valueOf(pageRecords));
       model.addAttribute("pageInfo",pageInfo);
        return "user/showByPage";
    }

    // 处理日期问题
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

}
